package com.datasync.core;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import javax.sql.DataSource;

public class DataSourceFactory {
    public static DataSource create(DataSourceConfig config) {
        return DataSourceBuilder.create()
                .type(HikariDataSource.class)
                .url(buildJdbcUrl(config))
                .username(config.getUsername())
                .password(config.getPassword())
                .build();
    }

    private static String buildJdbcUrl(DataSourceConfig config) {
        switch (config.getType()) {
            case GAUSSDB:
                return config.getJdbcUrl().replace("{host}", config.getHost())
                        .replace("{port}", String.valueOf(config.getPort()))
                        .replace("{database}", config.getDatabase());
            case TDSQL:
                return config.getJdbcUrl().replace("{host}", config.getHost())
                        .replace("{port}", String.valueOf(config.getPort()))
                        .replace("{database}", config.getDatabase());
            default:
                throw new IllegalArgumentException("Unsupported database type: " + config.getType());
        }
    }
}